<?php
/**
 * Author: qiba@ibantang.com, 2019, all rights reserved.
 * Date: 2019-04-19
 * Time: 20:39
 */

namespace Core\Common\Helper;

use Core\Common\Base\RoundRobin;
use Core\Common\Base\Service;

class GrpcService
{
    /**
     * 获取grpc服务
     * @param $service
     * @return array|null
     */
    public static function getService($service)
    {
        $targets = Service::getService($service, GatewayService::PROTO_GRPC);
        if ($targets && is_array($targets)) {
            $c = count($targets);
            if (1 == $c) {
                return $targets[0];
            }

            $srvId = Service::getServiceId($service, GatewayService::PROTO_GRPC);
            $index = RoundRobin::incr($srvId);
            return $targets[$index % $c];
        }
        return null;
    }
}
